<template>
  <!-- 学校学院管理 -->
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        class="formBox"
        label-width="100px"
      >
        <el-form-item label="所属校区" prop="campusId">
          <el-select v-model="queryParams.campusId" placeholder="请选择所属校区">
            <el-option
              v-for="(item, index) in getCampusListData"
              :key="index"
              :label="item.campusName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学院状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择学院状态">
            <el-option label="启用" value="1" />
            <el-option label="停用" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 按钮 -->
    <el-card class="box-card" style="margin-top: 10px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="buttonMethod(1)"
            v-hasPermi="['educational:college:add']"
            >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['educational:college:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <!-- 列表 -->
    <el-card class="table-card" style="margin-top: 10px">
      <el-table :data="dataList" stripe border>
        <el-table-column label="所属校区" prop="campusName" width="200" />
        <el-table-column label="学院/院系" prop="collegeName" />
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '1'">启用</el-tag>
            <el-tag v-if="scope.row.status == '2'" type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="buttonMethod(1, scope.row)"
              v-hasPermi="['educational:college:edit']"
              >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="buttonMethod(4, scope.row)"
              v-hasPermi="['educational:college:del']"
              >删除
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="buttonMethod(5, scope.row)"
              v-hasPermi="['educational:college:status']"
            >
              <span v-if="scope.row.status == 1">停用</span>
              <span v-if="scope.row.status == 2">启用</span>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加、编辑 -->
    <el-dialog :title="title" :visible.sync="status" width="30%">
      <el-form label-width="120px" size="small" class="formBox">
        <el-row class="mb_10">
          <el-col :span="20">
            <el-form-item label="所属校区：" class="is-required">
              <el-select
                v-model="queryForm.campusId"
                placeholder="请选择所属校区"
                class="width_100"
              >
                <el-option
                  v-for="(item, index) in getCampusListData"
                  :key="index"
                  :label="item.campusName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="学院名称：" class="is-required">
              <el-input
                placeholder="请输入学院名称"
                v-model="queryForm.collegeName"
                maxlength="50"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="状态：" class="is-required">
              <el-radio-group v-model="queryForm.status">
                <el-radio label="1">启用</el-radio>
                <el-radio label="2">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="buttonMethod(2)">取 消</el-button>
        <el-button type="primary" @click="buttonMethod(3)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  collegeList,
  getCampusList,
  collegePost,
  collegePut,
  collegeDelete,
} from "@/api/educational";
export default {
  name: "",
  data() {
    return {
      status: false,
      title: "",
      dataList: [],
      queryParams: { pageNum: 1, pageSize: 10, campusId: null, status: null },
      queryForm: {},
      total: 1,
      showSearch: true,
      getCampusListData: [],
    };
  },
  created() {
    this.getList();
    this.getCampusListFun();
  },
  methods: {
    // 获取列表数据
    getList() {
      var data = this.queryParams;
      collegeList(data).then((res) => {
        if (res.code == 200) {
          this.dataList = res.rows;
          this.total = res.total;
        }
      });
    },
    /**
     * 获取校园列表
     */
    getCampusListFun() {
      getCampusList().then((res) => {
        if (res.code == 200) {
          this.getCampusListData = res.data;
        }
      });
    },
    /**
     * 按钮点击事件处理方法
     */
    buttonMethod(options, value) {
      //options 等于 1 添加按钮
      if (options == 1) {
        if (value) {
          this.queryForm = { ...value };
          this.title = "修改";
        } else {
          this.queryForm = {};
          this.title = "添加";
        }
        this.status = true;
      }
      //options 等于 2 关闭弹窗
      else if (options == 2) {
        this.queryForm = {};
        this.title = "";
        this.status = false;
      }
      //options 等于 3 添加修改
      else if (options == 3) {
        var data = this.queryForm;
        if (!data.campusId) {
          return this.$message({
            type: "error",
            message: "请选择校区名称",
          });
        }
        if (!data.collegeName) {
          return this.$message({
            type: "error",
            message: "请输入学院名称",
          });
        }
        if (!data.status) {
          return this.$message({
            type: "error",
            message: "请选择状态",
          });
        }
        if (data.id) {
          collegePut(data).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.queryForm = {};
              this.title = "";
              this.status = false;
              this.getList();
            } else {
              this.$message({
                type: "success",
                message: res.msg,
              });
            }
          });
        } else {
          collegePost(data).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.queryForm = {};
              this.title = "";
              this.status = false;
              this.getList();
            } else {
              this.$message({
                type: "success",
                message: res.msg,
              });
            }
          });
        }
      }
      //删除该学院信息
      else if (options == 4) {
        this.$confirm("确定删除该学院信息吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          collegeDelete(value.id).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        });
      }
      //关闭/启用该学院
      else if (options == 5) {
        let text = value.status == "1" ? "停用" : "启用";
        this.$confirm("确定" + text + "该学院吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          collegePut({
            id: value.id,
            status: value.status == "1" ? "2" : "1",
          }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.getList();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        });
      }
    },
    /**
     * 处理查询请求
     *
     * 将查询参数中的页码重置为1，并调用getList方法获取列表数据
     */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询表单并重新执行查询操作
     */
    resetQuery() {
      this.queryParams = {
        campusId: "",
        pageNum: 1,
        pageSize: 10,
        campusId: null,
        status: null,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**处理导出操作*/
    handleExport() {
      this.download(
        "/edu/college/export",
        {
          ...this.queryParams,
        },
        `学校学院管理.xlsx`
      );
    },
  },
};
</script>
